package com.kqkj.dyrule.marketing.utils;

import com.kqkj.dyrule.marketing.beans.EventBean;
import com.kqkj.dyrule.marketing.beans.EventCondition;

import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EventUtil {
    /**
     * 事件与规则的比较
     * @param eventBean 触发的事件
     * @param triggerEvent 规则
     * @return
     */
    public static boolean compare(EventBean eventBean, EventCondition triggerEvent) {
        if (eventBean.getEventId().equals(triggerEvent.getEventId())){
            Map<String, String> eventProps = triggerEvent.getEventProps();
            Set<String> keys = eventProps.keySet();
            for (String key : keys) {
                String value = eventBean.getProperties().get(key);
                if (!eventProps.get(key).equals(value)) return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 正则匹配，返回匹配成功的个数
     * @param eventStr
     * @param pattern
     * @return
     */
    public static int sequenceStrMatchRegexGroupCount(String eventStr,String pattern){
        Pattern compile = Pattern.compile(pattern);
        Matcher matcher = compile.matcher(eventStr);
        int count = 0;
        while (matcher.find()) count++;
        return count;
    }
}
